﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _4Gewinnt
{
    /// <summary>
    /// An engine controlled by a human
    /// </summary>
    public class EngineHuman : Engine
    {
        private int currentMove;
        private bool GUIMoveReceived = false;
        private Board board;
        private GameColor op;

        /// <summary>
        /// Initializes the engine
        /// </summary>
        /// <param name="me">The engine's game color</param>
        public EngineHuman(GameColor me)
        {
            this._me = me;
            if (_me == GameColor.Blue)
                op = GameColor.Red;
            else
                op = GameColor.Blue;
            this._isHuman = true;
            this.board = new Board(4);
        }

        /// <summary>
        /// Waits until the GUI called receiveMoveFromGUI
        /// </summary>
        /// <returns>A move</returns>
        public override int makeMove()
        {
            WaitForMove:
            while (!GUIMoveReceived)
                Application.DoEvents();
            GUIMoveReceived = false;

            if (!board.isLegalColumn(currentMove))
                goto WaitForMove;
            else
            {
                //mark move in board
                board.moveTo(_me, currentMove);
                return currentMove;
            }
        }

        /// <summary>
        /// Takes a move from the GUI
        /// </summary>
        /// <param name="c">The column to put a token in</param>
        public void receiveMoveFromGUI(int c)
        {
            GUIMoveReceived = true;
            currentMove = c;
        }

        /// <summary>
        /// Learn the opponent's move
        /// </summary>
        /// <param name="c">The column the opponent has moved to</param>
        public override void update(int c)
        {
            board.moveTo(op, c);
        }

        /// <summary>
        /// Called after each engine's turn. null means "nothing to say"
        /// </summary>
        /// <returns>A message a engine can send after each own turn</returns>
        public override string say()
        {
            return null;
        }

        /// <summary>
        /// Return Engine Name
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "Human";
        }

    }
}
